package com.smart.community.region.controller;

import com.smart.community.commons.result.Result;
import com.smart.community.region.service.IHouseholdAggregationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 住户聚合查询控制器
 * 
 * @author Wu.Liang
 * @since 2025-07-03
 */
@Slf4j
@RestController
@RequestMapping("/region/household/aggregation")
@Tag(name = "住户聚合查询", description = "住户统计信息聚合查询相关接口")
public class HouseholdAggregationController {

	@Autowired
	private IHouseholdAggregationService householdAggregationService;

	// ==================== 统计查询接口 ====================

	/**
	 * 获取住户基础统计信息
	 * 
	 * @throws Exception
	 */
	@GetMapping("/basic-stats")
	@Operation(summary = "获取住户基础统计信息", description = "根据社区ID获取住户基础统计信息")
	public Result<Map<String, Object>> getBasicStats(
			@Parameter(description = "社区ID列表，逗号分隔") @RequestParam String communityIds) throws Exception {
		List<Long> communityIdList = parseCommunityIds(communityIds);
		Map<String, Object> stats = householdAggregationService.getBasicStats(communityIdList);
		return Result.success(stats);
	}

	/**
	 * 获取住户详细统计信息
	 * 
	 * @throws Exception
	 */
	@GetMapping("/detailed-stats")
	@Operation(summary = "获取住户详细统计信息", description = "根据社区ID获取住户详细统计信息")
	public Result<Map<String, Object>> getDetailedStats(
			@Parameter(description = "社区ID列表，逗号分隔") @RequestParam String communityIds) throws Exception {
		List<Long> communityIdList = parseCommunityIds(communityIds);
		Map<String, Object> stats = householdAggregationService.getDetailedStats(communityIdList);
		return Result.success(stats);
	}

	/**
	 * 获取住户分布统计
	 * 
	 * @throws Exception
	 */
	@GetMapping("/distribution-stats")
	@Operation(summary = "获取住户分布统计", description = "根据社区ID获取住户分布统计信息")
	public Result<Map<String, Object>> getDistributionStats(
			@Parameter(description = "社区ID列表，逗号分隔") @RequestParam String communityIds) throws Exception {
		List<Long> communityIdList = parseCommunityIds(communityIds);
		Map<String, Object> stats = householdAggregationService.getDistributionStats(communityIdList);
		return Result.success(stats);
	}

	/**
	 * 获取住户趋势分析
	 * 
	 * @throws Exception
	 */
	@GetMapping("/trend-analysis")
	@Operation(summary = "获取住户趋势分析", description = "根据社区ID获取住户趋势分析数据")
	public Result<Map<String, Object>> getTrendAnalysis(
			@Parameter(description = "社区ID列表，逗号分隔") @RequestParam String communityIds,
			@Parameter(description = "时间范围：7-近7天，30-近30天，90-近90天") @RequestParam(defaultValue = "30") Integer days)
			throws Exception {
		List<Long> communityIdList = parseCommunityIds(communityIds);
		Map<String, Object> analysis = householdAggregationService.getTrendAnalysis(communityIdList, days);
		return Result.success(analysis);
	}

	// ==================== 私有方法 ====================

	/**
	 * 解析社区ID列表
	 */
	private List<Long> parseCommunityIds(String communityIds) {
		if (!StringUtils.hasText(communityIds)) {
			throw new IllegalArgumentException("参数communityIds不能为空");
		}

		List<Long> communityIdList = new ArrayList<>();
		for (String id : communityIds.split(",")) {
			if (StringUtils.hasText(id)) {
				communityIdList.add(Long.valueOf(id.trim()));
			}
		}

		if (communityIdList.isEmpty()) {
			throw new IllegalArgumentException("有效的社区ID不能为空");
		}

		return communityIdList;
	}
}